package com.zhuiYun.project;

import com.zhuiYun.project.api.commoditydetail.entity.Commodity;
import com.zhuiYun.project.api.commoditydetail.mapper.CommodityMapper;
import com.zhuiYun.project.api.seckill.entity.SeckillEntity;
import com.zhuiYun.project.api.seckill.mapper.SeckillMapper;
import com.zhuiYun.project.common.response.CommonConstants;
import com.zhuiYun.project.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * MapperScan 中如果时多个包可以使用数据 如：@MapperScan({"com.zhuiYun.project.*.mapper","com.zhuiYun.project.*.mapper"})
 * 项目启动类  本项目将作为模板项目
 * 技术包括 springboot+tk_mybatis+mybatis+mysql+redis+druid+spring security + jwt + swagger  mq + es
 */
@SpringBootApplication
@MapperScan({"com.zhuiYun.project.api.*.mapper","com.zhuiYun.project.api.*.*.mapper"})
public class AbsolutuniqApplication implements ApplicationRunner {
    @Resource
    SeckillMapper seckillMapper;

    @Autowired
    CommodityMapper commodityMapper;

    public static void main(String[] args) {
        SpringApplication.run(AbsolutuniqApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<SeckillEntity> seckillEntityAll = seckillMapper.selectAll();
        List<Commodity> commodities = commodityMapper.selectAll();
        //秒杀商品库存存入Redis
        for (SeckillEntity seckillEntity :
                seckillEntityAll) {
            RedisUtil.set(CommonConstants.COMMODITY_SECKILL_STOCK+seckillEntity.getCommId(),seckillEntity.getSeckillStock(),3600L);
        }
        //商品库存存入Redis
        for (Commodity commodity:
                commodities) {
            RedisUtil.set(CommonConstants.COMMODITY_STOCK+commodity.getId(),commodity.getGoodsQuantity(),3600L);
        }
    }
}
